﻿using StaffingSystem.EF;
using StaffingSystem.Tool;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace StaffingSystem.Controllers
{
    public class AdddepartmentController : Controller
    {
        // GET: adddepartment
        public ActionResult Section()
        {
            return View();
        }
        [HttpPost]
        //添加部门
        public ActionResult Section(Department ta)
        {
            Management db = new Management();
            JsonResult result = new JsonResult();
            db = new Management();
            //使用拼接字符串的方式，将要执行的存储过程名称以及参数拼接在一起
            StringBuilder procString = new StringBuilder();
            procString.Append("Department_insert");
            procString.Append(" @D_Pids,");
            procString.Append("@D_Numbers,");
            procString.Append("@D_Names");
            SqlParameter[] sq = {
                    new SqlParameter("@D_Pids",ta.D_PId),
                    new SqlParameter("@D_Numbers",ta.D_Number),
                    new SqlParameter("@D_Names",ta.D_Name),               
              };
            int a = db.Database.ExecuteSqlCommand(procString.ToString(), sq);
            if (a > 0)
            {
                result.Data = new
                {
                    State = "1",
                    Message = "添加成功"
                };
            }
            else
            {
                result.Data = new
                {
                    State = "2",
                    Message = "添加失败"
                };
            }
            return result;

        }

        //父级下拉框
        [HttpPost]
        public ActionResult Select()
        {
            Management db = new Management();
            List<Department> tt = new List<Department>();
            tt = db.Department.ToList();
            JsonResult result = new JsonResult();
            result.Data = new
            {
                data = JsonTool.JsonSerializer(tt)
            };
            return result;
        }



        //添加职位
        public ActionResult PositionS()
        {
            return View();
        }

        [HttpPost]
        public ActionResult PositionS(Entityone ta)
        {
            //接收AJAX的数据
            int ass = Convert.ToInt32( Request.Form["DepartmentId"]);
            string bs = Request.Form["P_Name"];

            //将值赋到新的实体类MODEL
            ta.DepartmentId = ass;
            ta.P_Name = bs;

            Management db = new Management();
            JsonResult result = new JsonResult();
            db = new Management();
            //使用拼接字符串的方式，将要执行的存储过程名称以及参数拼接在一起
            StringBuilder procString = new StringBuilder();
            procString.Append("Position_insert");
            procString.Append(" @pname,");
            procString.Append("@DepartmentId");

            SqlParameter[] sq = {
                    new SqlParameter("@pname",ta.P_Name),
                    new SqlParameter("@DepartmentId",ta.DepartmentId),
              };
            int a = db.Database.ExecuteSqlCommand(procString.ToString(), sq);
            if (a > 0)
            {
                result.Data = new
                {
                    State = "1",
                    Message = "添加成功"
                };
            }
            else
            {
                result.Data = new
                {
                    State = "2",
                    Message = "添加失败"
                };
            }
            return result;
        }





        //所属部门下拉框
        [HttpPost]
        public ActionResult Selects2()
        {
            Management db = new Management();
            List<Department> tt = new List<Department>();
            tt = db.Department.ToList();
            JsonResult result = new JsonResult();
            result.Data = new
            {
                data = JsonTool.JsonSerializer(tt)
            };
            return result;
        }


    }
}